cd "/Users/mmoral/Dropbox/--Tamamlanan Çalışma ve Yayınlar/--Elections and Public Opinion in Turkey_Through the Prism of the 2018 Elections/-Replication/Politics as (Un)Usual? An Overview of the June 2018 Presidential and Parliamentary Elections in Turkey"
import excel "June 2018 Polls.xlsx", sheet("Sheet1") firstrow case(lower) clear

drop source*
rename (undecided q x) (undecided_#), addnumber
local list1= "erdogan ince aksener demirtas karamollaoglu perincek"
local list2= "cumhurittifak milletittifak hdpittifak"
local list3= "akp mhp chp iyi saadet hdp"

forval i=1/3{														/* Proportional allocation of undecided voters (for consistency across reported polling estimates) */
replace undecided_`i'="" if undecided_`i'=="Yes/Not coded"
encode undecided_`i', gen(und`i')
foreach var of varlist `list`i''{
replace `var'=`var'+`var'/(100-und`i') if !missing(und`i')
}
}

drop if exceldate<43210												/* Dropping polls conducted before the announcement of the early elections */
gen date_stata=exceldate+td(30dec1899)
format date_stata %td

encode firm, generate(house)
by house, sort: gen count=_n										/* Counting the number of polls conducted by each pollster */
recode n (missing=1000)												/* Recoding missing sample sizes as 1000 */	
gen weight=n

gen cumit=cumhurittifak 
gen milit=milletittifak
replace cumit=akp+mhp if missing(cumit)
replace milit=chp+iyi+saadet if missing(milit)
replace milit=chp+iyi if missing(milit)

* Polling Estimates of Electoral Alliance, Party, and Candidate Vote Shares (Figure 1)
sum cumit milit hdp
ttable 33

foreach ittifak in cumit milit hdp {
running `ittifak' date_stata [w=count], ci xlab(21294(30)21349) gen(f_`ittifak') gense(sf_`ittifak') nograph
gen ub_f_`ittifak'=2.035*sf_`ittifak'+f_`ittifak'
gen lb_f_`ittifak'=-2.035*sf_`ittifak'+f_`ittifak'
}

twoway ///
(line f_cumit date_stata, sort lcolor(orange%50)) 	(rarea ub_f_cumit lb_f_cumit date_stata, sort color(orange%25) lwidth(none)) 	(scatter cumit date_stata [aw=weight], mcolor(orange%35) msymbol(smcircle) msize(tiny)) ///
(line f_milit date_stata, sort lcolor(red%50)) 		(rarea ub_f_milit lb_f_milit date_stata, sort color(red%25) lwidth(none)) 		(scatter milit date_stata [aw=weight], mcolor(red%35) msymbol(smsquare) msize(tiny)) ///
(line f_hdp date_stata, sort lcolor(green%50)) 		(rarea ub_f_hdp lb_f_hdp date_stata, sort color(green%25) lwidth(none)) 		(scatter hdp date_stata [aw=weight], mcolor(green%35) msymbol(smtriangle) msize(tiny)) ///
if date_stata>=21299, scheme(mmoral3) yline(53.48, lcolor(orange%50) lpattern(shortdash) noextend) yline(33.3, lcolor(red%50) lpattern(shortdash) noextend) yline(11.53, lcolor(green%50) lpattern(shortdash) noextend) xlab(21299(14)21355) xsca(r(21299 21356)) ///
legend(order(1 "Ak Parti + MHP" 4 "CHP + SP + İyi Parti" 7 "HDP") pos(6) rows(1)) ylab(5(10)55) ///
xtitle("") ytitle("Alliance/Party Vote Share (%)", height(4)) name(Figure1, replace) nodraw

running erdogan date_stata [w=weight], ci xlab(21299(14)21355) gen(f_erd) gense(sf_erd) nograph	
running ince date_stata [w=weight], ci xlab(212992129914)21355) gen(f_inc) gense(sf_inc) nograph
running aksener date_stata [w=weight], ci xlab(21299(14)21355) gen(f_aks) gense(sf_aks) nograph
running demirtas date_stata [w=weight], ci xlab(21299(14)21355) gen(f_dem) gense(sf_dem) nograph

sum erd inc aks dem
ttable 35

foreach x in erd inc aks dem{
gen ub_f_`x'=2.03*sf_`x'+f_`x'
gen lb_f_`x'=-2.03*sf_`x'+f_`x'
}

twoway ///
(line f_erd date_stata, sort lcolor(orange%50)) (rarea ub_f_erd lb_f_erd date_stata, sort color(orange%25) lwidth(none)) 	(scatter erdogan date_stata [aw=weight], mcolor(orange%35) msymbol(smcircle) msize(tiny)) ///
(line f_inc date_stata, sort lcolor(red%50)) 	(rarea ub_f_inc lb_f_inc date_stata, sort color(red%25) lwidth(none)) 		(scatter ince date_stata [aw=weight], mcolor(red%35) msymbol(smsquare) msize(tiny)) ///
(line f_aks date_stata, sort lcolor(blue%50)) 	(rarea ub_f_aks lb_f_aks date_stata, sort color(blue%25) lwidth(none)) 		(scatter aksener date_stata [aw=weight], mcolor(blue%35) msymbol(smdiamond) msize(tiny)) ///
(line f_dem date_stata, sort lcolor(green%50)) 	(rarea ub_f_dem lb_f_dem date_stata, sort color(green%25) lwidth(none)) 	(scatter demirtas date_stata [aw=weight], mcolor(green%35) msymbol(smtriangle) msize(tiny)) ///
if date_stata>=21299, scheme(mmoral3) yline(52.38, lcolor(orange%50) lpattern(shortdash) noextend) yline(30.79, lcolor(red%50) lpattern(shortdash) noextend) yline(7.42, lcolor(blue%50) lpattern(shortdash) noextend) ///
yline(8.32, lcolor(green%50) lpattern(shortdash) noextend) xlab(21299(14)21355) xsca(r(21299 21356)) legend(order(1 "Recep Tayyip Erdoğan" 4 "Muharrem İnce" 7 "Meral Akşener" 10 "Selahattin Demirtaş") pos(6) rows(1)) ylab(5(10)55) ///
xtitle("Poll Date", height(4)) ytitle("Candidate Vote Share (%)", height(4)) name(Figure2, replace) ///
note("{it:Notes:} Markers indicate individual polls (weighted by and proportional to reported sample size). Dashed lines show observed (domestic) election outcome and solid lines" "show mean symmetric nearest neighbour smoothing estimates with confidence intervals at 95% level.") nodraw

graph combine Figure1 Figure2, cols(1) ycommon iscale(*1.75) graphregion(margin(zero)) altshrink
gr_edit .plotregion1.graph1.yaxis1.title.style.editstyle margin(vsmall)
gr_edit .plotregion1.graph2.yaxis1.title.style.editstyle margin(small)
gr_edit .plotregion1.graph1.style.editstyle margin(vsmall)
gr_edit .plotregion1.graph2.style.editstyle margin(vsmall)
gr_edit .plotregion1.graph2.xaxis1.title.style.editstyle margin(small)
gr_edit .plotregion1.graph2.note.style.editstyle size(small) 
gr export "Figure1.pdf", replace

** Mean Absolute Errors in Last Publicly Available Polls (Appendix A3)
gen akp_act=42.28
gen mhp_act=11.2
gen hdp_act=11.53
gen chp_act=22.8
gen saadet_act=1.36
gen iyi_act=10.14

gen erdogan_act=52.38
gen aksener_act=7.42
gen ince_act=30.79
gen demirtas_act=8.32
gen karamollaoglu_act=0.9
gen perincek_act=0.2

gen cumhurittifak_act=53.48
gen milletittifak_act=33.3

gsort firm -date
by firm: gen order=_n
by firm: gen total=_N

foreach var of varlist erdogan ince aksener demirtas akp chp hdp mhp iyi saadet cumhurittifak milletittifak{
gen dif_`var'=abs(`var'_act-`var') if !missing(`var')
}
egen meanabs_pres=rowmean(dif_erdogan dif_ince dif_aksener dif_demirtas)
egen meanabs_parl=rowmean(dif_akp dif_mhp dif_chp dif_hdp dif_iyi)
egen meanabs_itt=rowmean(dif_cumhurittifak dif_milletittifak dif_hdp) if missing(saadet)
gen margin_of_error_95=100*1.96*sqrt(.5*(1-.5)/n)

preserve
keep if order==1 & total!=1 & exceldate>=43252
list firm date n meanabs*
restore
